package com.jumi.microservice.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jumi.microservice.common.base.dto.BasePageResponse;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.core.aspect.WebLog;
import com.jumi.microservice.common.utils.ResultGenerator;
import com.jumi.microservice.constant.ExceptionEnum;
import com.jumi.microservice.domain.Log;
import com.jumi.microservice.domain.LogDetailVO;
import com.jumi.microservice.domain.OperateLogQuery;
import com.jumi.microservice.domain.RotationChart;
import com.jumi.microservice.domain.dto.RotationChartDTO;
import com.jumi.microservice.domain.vo.RotationChartVO;
import com.jumi.microservice.service.LogService;
import com.jumi.microservice.service.RotationChartService;
import com.jumi.microservice.utils.LogObjectCopyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.apache.commons.lang.StringUtils;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api(tags = "轮播图管理相关接口")
@RequestMapping("/auth/RotationChart")
public class RotationChartController {

    @Autowired
    private RotationChartService rotationChartService;
    @Autowired
    private LogService logService;

    @ApiOperation(value = "新增",notes = "轮播图管理表插入数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "标题",value = "title"),
            @ApiImplicitParam(name = "类型",value = "type(0:商品,1:活动,2:其他)"),
            @ApiImplicitParam(name = "商品id",value = "goodsId"),
            @ApiImplicitParam(name = "链接商品",value = "ConnectGoods"),
            @ApiImplicitParam(name = "排序",value = "sort"),
            @ApiImplicitParam(name = "轮播图",value = "image")
    })
    @PostMapping("/add")
    public ResponseResult add(@Validated @RequestBody RotationChartVO vo, HttpServletRequest request){
        RotationChartDTO dto = vo2Dto(vo);
        rotationChartService.insert(dto,request);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "编辑",notes = "编辑数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "id"),
            @ApiImplicitParam(name = "标题",value = "title"),
            @ApiImplicitParam(name = "类型",value = "type(0:商品,1:活动,2:其他)"),
            @ApiImplicitParam(name = "商品id",value = "goodsId"),
            @ApiImplicitParam(name = "链接商品",value = "ConnectGoods"),
            @ApiImplicitParam(name = "排序",value = "sort"),
            @ApiImplicitParam(name = "轮播图",value = "image")
    })
    @PutMapping("/update")
    public ResponseResult update(@Validated @RequestBody RotationChartVO vo,HttpServletRequest request){
        RotationChartDTO dto = vo2Dto(vo);
        rotationChartService.update(dto,request);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "数据查询", notes = "根据id单条数据查询")
    @ApiImplicitParam(name="id",value = "id")
    @GetMapping("/detail")
    public ResponseResult<RotationChartVO> detail(Long id){
       RotationChartDTO dto = rotationChartService.selectById(id);
        return ResultGenerator.genSuccessResult(dto2Vo(dto));
    }

    @ApiOperation(value = "删除数据", notes = "删除指定ID数据")
    @ApiImplicitParam(name = "id", value = "ID")
    @DeleteMapping("/delete")
    public ResponseResult delete(Long id,HttpServletRequest request){
        rotationChartService.delete(id,request);
        return ResultGenerator.genSuccessResult();
    }

    @ApiOperation(value = "启用/停用", notes = "状态的启用/停用")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "ID"),
            @ApiImplicitParam(name = "status", value = "状态，1启用，0.禁用")
    })

    @PutMapping("/enableStatus")
    @WebLog(description = "更新状态的启用/停用")
    public ResponseResult enableStatus(@RequestParam Long id, Integer status, HttpServletRequest request) {
        rotationChartService.enableStatus(id, status, request);
        return ResultGenerator.genSuccessResult();
    }


    @ApiOperation(value = "查询分页", notes = "获取轮播图表分页列表数据")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "分页大小"),
    })
    @GetMapping("/list")
    public BasePageResponse list(
            @RequestParam(defaultValue = "0") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            RotationChartVO vo) {
        LambdaQueryWrapper<RotationChart> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(vo.getTitle()),RotationChart::getTitle,vo.getTitle());
        wrapper.eq(vo.getStatus()!=null,RotationChart::getStatus,vo.getStatus());
        wrapper.ge(StringUtils.isNotEmpty(vo.getStartTimeAdd()), RotationChart::getCreateTime, vo.getStartTimeAdd());
        wrapper.le(StringUtils.isNotEmpty(vo.getEndTimeAdd()), RotationChart::getCreateTime, vo.getEndTimeAdd());
        wrapper.ge(StringUtils.isNotEmpty(vo.getStartTimeUp()), RotationChart::getUpdateTime, vo.getStartTimeUp());
        wrapper.le(StringUtils.isNotEmpty(vo.getEndTimeUp()), RotationChart::getUpdateTime, vo.getEndTimeUp());
        wrapper .eq(RotationChart::getIsDel, 0);
        wrapper.orderByDesc(RotationChart::getSort, RotationChart::getUpdateTime);
        IPage<RotationChart> pageInfo = rotationChartService.page(
                new Page<>(page, size), wrapper);
        return ResultGenerator.genSuccessResultPage(pos2Vos(pageInfo.getRecords()), pageInfo.getTotal());

    }

    @ApiOperation(value = "模块操作日志查询", notes = "模块操作日志查询分页列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页码"),
            @ApiImplicitParam(name = "size", value = "分页大小"),
    })
    @GetMapping("/listLog")
    public BasePageResponse listLog(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "0") Integer size,
            OperateLogQuery logQuery) {
        Page<Log> list = logService.listOperateLogs(logQuery);
        //处理分页
//        if(list.size()>size){
//            return ResultGenerator.genSuccessResultPage(
//                    list.stream()
//                            .skip(size * (page - 1))
//                            .limit(size).collect(Collectors.toList())
//                    ,Long.valueOf(list.size()));
//        }
        return ResultGenerator.genSuccessResultPage(LogObjectCopyUtil.convertOperateLogDoList2VoList(list.getRecords()), list.getTotal());
    }


    /**
     * 部门DO对象转换 do->vo
     *
     * @param rotationCharts 部门do集合
     * @return 部门VO集合
     */
    private List<RotationChartVO> pos2Vos(List<RotationChart> rotationCharts) {
        List<RotationChartVO> pos = rotationCharts.stream().map(p -> {
            RotationChartVO vo = null;
            try {
                vo = p.clone(RotationChartVO.class);
            } catch (Exception e) {
                ExceptionEnum.FAIL_CHANGE.newException();
            }
            return vo;
        }).collect(Collectors.toList());
        return pos;
    }

    /**
     * 部门对象转换 dto->vo
     *
     * @param dto 部门dto
     * @return 部门vo
     */
    private RotationChartVO dto2Vo(RotationChartDTO dto) {
        RotationChartVO vo = null;
        try {
            vo = dto.clone(RotationChartVO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return vo;
    }


    /**
     * 部门VO对象转换 vo->dto
     *
     * @param vo 部门VO
     * @return 部门VO
     */
    private RotationChartDTO vo2Dto(RotationChartVO vo) {
        RotationChartDTO dto = null;
        try {
            dto = vo.clone(RotationChartDTO.class);
        } catch (Exception e) {
            ExceptionEnum.FAIL_CHANGE.newException();
        }
        return dto;
    }
}
